/**
 * Copyright 2013 91kge.com
 *
 *
 * Create on 2013-1-10 下午2:42:19
 */
package xj.toolkit.redis;

import junit.framework.Assert;
import junit.framework.TestCase;
import redis.clients.jedis.Jedis;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 * @author <a href="mailto:xj.cao@91kge.com>xiaojian.cao</a>
 * 
 */
public class MultiThreadTestJedis2Pool extends TestCase {

	private ApplicationContext ac = null;

	@Before
	protected void setUp() throws Exception {
		ac = new ClassPathXmlApplicationContext("classpath:testJedis2Pool.xml");
	}
	
	public void testSimple() {
		final Jedis2Commands jedis2 = (Jedis2Commands) ac.getBean("jedis");
		jedis2.set("key_1", "value_1");
		assertEquals(jedis2.get("key_1"), "value_1");
	}

	@Test
	public void testMultiThreadJedis2() {
		final Jedis2Commands jedis2 = (Jedis2Commands) ac.getBean("jedis");
		for (int i = 0; i < 50; i++) {
			final int ii = i;
			new Thread(new Runnable() {
				public void run() {
					for (int j = 0; j < 100000; j++) {
						jedis2.set("key" + ii + j, "value" + ii + j);
						Assert.assertEquals("value" + ii + j, jedis2.get("key" + ii + j));
					}
				}
			}).start();

		}

		try {
			Thread.sleep(20 * 1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	@After
	protected void tearDown() throws Exception {
		super.tearDown();
	}

}
